/**
 * Copyright 2024 The coretext Authors. All Rights Reserved.
 */
import display from '@ohos.display'
import MeasureText from '@ohos.measure';

interface Rect {
  left: number,
  top: number,
  right: number,
  bottom: number
}

export class UIUtils {
  public static getScreenWidth(): number {
    return display.getDefaultDisplaySync().width;
  }

  public static getScreenHeight(): number {
    return display.getDefaultDisplaySync().height;
  }

  public static getTextWidth(text: string, fontSize: number): number {
    return MeasureText.measureText({ textContent: text, fontSize: fontSize });
  }

  public static getComponentByKey(id: string): any {
    const inspector = getInspectorByKey(id);
    // console.log("yangzc", inspector) Log length exceeds maximum
    // Object.getOwnPropertyNames(obj) 输出 $type,$ID,$rect,$attrs
    if (inspector) {
      return JSON.parse(inspector);
    }
  }

  public static getComponentRect(id: string): Rect {
    const inspector = UIUtils.getComponentByKey(id)
    if (inspector) {
      let rectInfo = JSON.parse('[' + inspector.$rect + ']')
      const rect_left = JSON.parse('[' + rectInfo[0] + ']')[0]
      const rect_top = JSON.parse('[' + rectInfo[0] + ']')[1]
      const rect_right = JSON.parse('[' + rectInfo[1] + ']')[0]
      const rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1]
      return {
        "left": rect_left, "top": rect_top, "right": rect_right, "bottom": rect_bottom
      }
    }
  }
}